package com.hy.controller;

import com.hy.Result.ResponseResult;
import com.hy.medicine.dingdan.vo.CartItemAddVO;
import com.hy.service.CartItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 购物车控制器
 */
@RestController
@RequestMapping("/api/cart")
@Api(tags = "购物车接口")
public class CartController {

    @Autowired
    private CartItemService cartItemService;

    /**
     * 添加商品到购物车
     */
    @PostMapping("/add")
    @ApiOperation("添加商品到购物车")
    public ResponseResult<?> addToCart(
            @RequestBody @Validated CartItemAddVO cartItemAddVO,
            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId) {
        return cartItemService.addToCart(cartItemAddVO, userId);
    }

    /**
     * 获取购物车列表
     */
    @GetMapping("/list")
    @ApiOperation("获取购物车列表")
    public ResponseResult<?> getCartList(
            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId) {
        return cartItemService.getCartList(userId);
    }

    /**
     * 更新购物车商品数量
     */
    @PutMapping("/update/{id}")
    @ApiOperation("更新购物车商品数量")
    public ResponseResult<?> updateCartItemQuantity(
            @ApiParam(value = "购物车项ID", required = true) @PathVariable("id") Long id,
            @ApiParam(value = "数量", required = true) @RequestParam("quantity") Integer quantity,
            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId) {
        return cartItemService.updateCartItemQuantity(id, quantity, userId);
    }

    /**
     * 删除购物车商品
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除购物车商品")
    public ResponseResult<?> deleteCartItem(
            @ApiParam(value = "购物车项ID", required = true) @PathVariable("id") Long id,
            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId) {
        return cartItemService.deleteCartItem(id, userId);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clear")
    @ApiOperation("清空购物车")
    public ResponseResult<?> clearCart(
            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId) {
        return cartItemService.clearCart(userId);
    }

    /**
     * 获取购物车商品数量
     */
    @GetMapping("/count")
    @ApiOperation("获取购物车商品数量")
    public ResponseResult<?> getCartCount(
            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId) {
        return cartItemService.getCartCount(userId);
    }
}
